package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.SysUserMapper;
import com.ktjiaoyu.service.SysUserService;
import com.ktjiaoyu.utils.Page;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @return 13141
 * 2025/6/24
 * 14:46
 */
@Service("sysUserService")
// 如果在类上添加@Transactional注解，则对本类中所有业务方法都有效
//@Transactional
public class SysUserServiceImpl implements SysUserService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SysUserServiceImpl.class);
    // 定义 Mapper
    @Resource
    private SysUserMapper sysUserMapper;
    // 提供 setter 方法用于注入属性
    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }
    // 查询用户列表
    @Override
    public List<SysUser> findList(SysUser sysUser) {
        return sysUserMapper.getSysUserList(sysUser);
    }

    // 根据供货商名模糊查询
    @Override
    // 若当前方法支持事务管理，则添加@Transactional注解
    @Transactional
    public int saveBatch(List<SysUser> sysUserList) {
        int rows = 0;
        for (int i = 0; i < sysUserList.size(); i++){
            // 拿出每一条用户数据插入
            SysUser sysUser = sysUserList.get(i);
            if (i == 1){
                // 插入第二条数据时，模拟数据库异常，查看事务是否生效
                throw new RuntimeException("数据库异常");
            }
            // 调用插入数据操作，计入总受影响操作
            rows += sysUserMapper.insert(sysUser);
        }
        // 返回总受影响操作
        return rows;
    }

    // 删除用户
    @Override
    @Transactional
    public int deleteUser(Long id) {
        return sysUserMapper.deleteUser(id);
    }

    // 修改用户
    @Override
    @Transactional
    public int updateUser(SysUser sysUser) {
        return sysUserMapper.updateUser(sysUser);
    }

    // 登录
    @Override
    public SysUser login(String account, String password) {
        SysUser loginUser = sysUserMapper.getByAccount(account);
        // 如果账号存在且密码相等, 则返回查到的登录用户信息
        if (loginUser != null && loginUser.getPassword().equals(password)){
            return loginUser;
        }
        return null;
    }
    // 多条件查询用户分页数据
    @Override
    public Page<SysUser> findPage(SysUser entity, Integer pageNo, Integer pageSize) throws Exception {
        Page<SysUser> page = null;
        try {
            page = new Page<SysUser>();
            page.setPageSize(pageSize);
//            查询总数，计算总页数
            Long count = sysUserMapper.count(entity);
            page.setTotalCount(count.intValue());
            page.setPageNo(pageNo);
//            查询分页数据
            List<SysUser> data = sysUserMapper.getPage(entity, page.offset(), page.getPageSize());
            page.setData(data);
        } catch (Exception e) {
            LOGGER.error("执行 SysUserServiceImpl.findPage(SysUser,Integer, Integer)方法出现异常.", e);
            throw e;
        }
        return page;
    }
//    保存或修改
    @Override
    public boolean saveOrUPdate(SysUser entity) throws Exception {
        try {
            if (entity.getId() == null) {
//                无主题则新增
                return sysUserMapper.insert(entity) > 0;
            } else {
//                有主题则修改
                return sysUserMapper.updateUser(entity) > 0;
            }
        } catch (Exception e) {
            LOGGER.error("执行 SysUserServiceImpl.saveOrUpdate(SysUser)方法出现异常，", e);
            throw e;
        }
    }
//    根据 id 查询
    @Override
    public SysUser findById(Long id) throws Exception {
        try {
            return sysUserMapper.getById(id);
        } catch (Exception e) {
            LOGGER.error("执行 SysUserServiceImpl" + ".findById(Long)方法出现异常.", e);
            throw e;
        }
    }
//    根据 account 查询用户
    @Override
    public SysUser findByAccount(String account) throws Exception {
        try {
            return sysUserMapper.getByAccount(account);
        } catch (Exception e) {
            LOGGER.error("执行 SysUserServiceImpl" + ".findByAccount(String)方法出现异常.", e);
            throw e;
        }
    }
}
